﻿using System;
using System.Linq;
using BLL;
using DataAccess;
using DevExpress.XtraEditors;
using QuanLyKhachSan.Validation;

namespace QuanLyKhachSan.FormThemCapNhat
{
    public partial class FormPhongCapNhat : XtraForm
    {
        public Phong phong = new Phong();
        PhongManager pManager = new PhongManager();
        public bool isInserted = false;
        public FormPhongCapNhat()
        {
            InitializeComponent();
        }

        public FormPhongCapNhat(Phong pCapNhat)
        {
            InitializeComponent();
            KhoiTaoPhong(pCapNhat);
        }

        private void KhoiTaoPhong(Phong p)
        {
            txtSoPhong.Text = p.SoPhong;
            lueditLoaiPhong.EditValue = p.MaLoaiPhong;
            cbbTrangThaiPhong.Text = p.TrangThaiPhong;
            numSucChua.Value = (int)p.SucChua;
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void FormPhongCapNhat_Load(object sender, EventArgs e)
        {
            lueditLoaiPhong.Properties.DataSource = new LoaiPhongManager().LayDanhSach().Result.ToList();
            Loading();
        }

        private void Loading()
        {
            if (System.String.Compare(btnThaoTac.Text, "Thêm", System.StringComparison.OrdinalIgnoreCase) == 0)
            {
                txtSoPhong.Text = "";
                lueditLoaiPhong.EditValue = "";
                cbbTrangThaiPhong.SelectedIndex = -1;
                numSucChua.Value = 0;
            }
        }

        private void btnThaoTac_Click_1(object sender, EventArgs e)
        {
            ThaoTac();
           
        }

        private void ThaoTac()
        {
            phong.SoPhong = this.txtSoPhong.Text;
            phong.MaLoaiPhong = this.lueditLoaiPhong.EditValue.ToString();
            phong.TrangThaiPhong = this.cbbTrangThaiPhong.Text;
            phong.SucChua = int.Parse(this.numSucChua.Text);
            ValidateResult vResult = pManager.Validate(phong);
            if (!vResult.Message.Any())
            {
                ActionResult aResult;
                if (System.String.Compare(btnThaoTac.Text, "Thêm", System.StringComparison.OrdinalIgnoreCase) == 0)
                {
                    aResult = pManager.Them(phong);
                }
                else
                {
                    aResult = pManager.CapNhat(phong);
                }
                if (!aResult.Exceptions.Any())
                {
                    isInserted = true;
                    this.Close();
                }
                else
                {
                    string s = "";
                    foreach (var ex in aResult.Exceptions)
                    {
                        s += ex.Message + "\n";
                    }
                    XtraMessageBox.Show(s);
                }
            }
            else
            {
                string s = "";
                foreach (var ex in vResult.Message)
                {
                    s += ex + "\n";
                }
                XtraMessageBox.Show(s);
            }
        }
    }
}
